package org.csu.mypetstore.api.controller.front;


import org.csu.mypetstore.api.common.CommonResponce;
import org.csu.mypetstore.api.entity.Category;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.pulsar.PulsarConnectionDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:8888",
        allowedHeaders = "*",
        methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS},
        allowCredentials = "true")
@RestController
@RequestMapping("/catalog/")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    //获得所有category大类
    @GetMapping("categorise")
    @ResponseBody
    public CommonResponce<List<Category>> getCategoryList(){
        return catalogService.getCategoryList();
    }

    //访问某个category大类的图片以及描述信息
    @GetMapping("categorise/{id}")
    @ResponseBody
    public CommonResponce<Category> getCategory(@PathVariable("id") String categoryId){
        return catalogService.getCategory(categoryId);
    }

    //访问某个category下的product列表
    @GetMapping("categories/{id}/products")
    @ResponseBody
    public  CommonResponce<List<Product>> getProductListByCategoryId(@PathVariable("id") String categoryId){
        return catalogService.getProductListByCategoryId(categoryId);
    }//这里是第一层跳转

    //渲染product下的item列表界面
    @GetMapping("products/{id}/items")
    @ResponseBody
    public CommonResponce<List<ItemVO>> getItemsByProductId(@PathVariable("id") String productId){
        return catalogService.getItemsByProductId(productId);
    }//第二层跳转

    //通过prodcutid获取该product的信息，渲染category下的product界面
    @GetMapping("products/{id}")
    @ResponseBody
    public CommonResponce<Product> getProductById(@PathVariable("id") String productId){
        return catalogService.getProductById(productId);
    }

    //获取item界面
    @GetMapping("products/items/{id}")
    @ResponseBody
    public CommonResponce<Item> getItemByItemid(@PathVariable("id") String itemId){
        return catalogService.getItemByItemid(itemId);
    }
    //实时搜索
    @PostMapping("search_products")
    @ResponseBody
    public CommonResponce<List<Product>> searchProductByKey(@RequestBody Map<String,String> request) {
        String keyword = request.get("keyword");// 从请求体中获取关键字
        return catalogService.searchProductByKey(keyword);
    }
}
